package com.wingsoft.homeFlow.service.impl;

import cn.hutool.db.DbUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wingsoft.common.constant.Constants;
import com.wingsoft.common.exception.ServiceException;
import com.wingsoft.common.utils.bean.BeanConvertUtils;
import com.wingsoft.homeFlow.bo.HomeFlowSourceBo;
import com.wingsoft.homeFlow.bo.HomeFlowSourceQo;
import com.wingsoft.homeFlow.bo.HomeFlowSourceVo;
import com.wingsoft.homeFlow.entity.HomeFlowSource;
import com.wingsoft.homeFlow.mapper.HomeFlowSourceMapper;
import com.wingsoft.homeFlow.service.HomeFlowSourceService;
import com.wingsoft.homeFlow.utils.DbUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author Wingsoft
 * @description 针对表【HOME_FLOW_SOURCE(流水线数据源)】的数据库操作Service实现
 * @createDate 2025-05-29 13:32:57
 */
@Service
public class HomeFlowSourceServiceImpl extends ServiceImpl<HomeFlowSourceMapper, HomeFlowSource> implements HomeFlowSourceService {

    @Transactional
    @Override
    public void add(HomeFlowSourceBo homeFlowSourceBo) {
        String name = homeFlowSourceBo.getName();
        if (StringUtils.isBlank(name)) {
            throw new ServiceException("数据源名称 不能为空");
        }
        Integer type = homeFlowSourceBo.getType();
        HomeFlowSourceBo.TypeEnum typeEnum = HomeFlowSourceBo.TypeEnum.getEnumByCode(type);
        String address = homeFlowSourceBo.getAddress();
        if (StringUtils.isBlank(address)) {
            throw new ServiceException("地址 不能为空");
        }
        if (typeEnum != HomeFlowSourceBo.TypeEnum.API) {
            String username = homeFlowSourceBo.getUsername();
            if (StringUtils.isBlank(username)) {
                throw new ServiceException("账号 不能为空");
            }
            String password = homeFlowSourceBo.getPassword();
            if (StringUtils.isBlank(password)) {
                throw new ServiceException("密码 不能为空");
            }
        }
        if (!this.save(BeanConvertUtils.convert(homeFlowSourceBo, HomeFlowSource.class))) {
            throw new ServiceException("新增失败");
        }
    }

    @Transactional
    @Override
    public void uptById(HomeFlowSourceBo homeFlowSourceBo) {
        String id = homeFlowSourceBo.getId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException("ID 不能为空");
        }
        String name = homeFlowSourceBo.getName();
        if (StringUtils.isBlank(name)) {
            throw new ServiceException("数据源名称 不能为空");
        }
        Integer type = homeFlowSourceBo.getType();
        HomeFlowSourceBo.TypeEnum typeEnum = HomeFlowSourceBo.TypeEnum.getEnumByCode(type);

        String address = homeFlowSourceBo.getAddress();
        if (StringUtils.isBlank(address)) {
            throw new ServiceException("地址 不能为空");
        }
        if (typeEnum != HomeFlowSourceBo.TypeEnum.API) {
            String username = homeFlowSourceBo.getUsername();
            if (StringUtils.isBlank(username)) {
                throw new ServiceException("账号 不能为空");
            }
            String password = homeFlowSourceBo.getPassword();
            if (StringUtils.isBlank(password) || Constants.PASSWORD_MASK.equals(password)) {
                homeFlowSourceBo.setPassword(null);
            }
        }
        if (!this.updateById(BeanConvertUtils.convert(homeFlowSourceBo, HomeFlowSource.class))) {
            throw new ServiceException("更新失败");
        }
    }

    @Transactional
    @Override
    public void delById(String id) {
        if (!this.removeById(id)) {
            throw new ServiceException("删除失败");
        }
    }

    @Override
    public Page<HomeFlowSourceVo> page(HomeFlowSourceQo homeFlowSourceQo) {
        String name = homeFlowSourceQo.getName();
        Integer type = homeFlowSourceQo.getType();
        List<Integer> typeList = homeFlowSourceQo.getTypeList();
        String driver = homeFlowSourceQo.getDriver();
        String address = homeFlowSourceQo.getAddress();

        LambdaQueryWrapper<HomeFlowSource> homeFlowSourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            homeFlowSourceLambdaQueryWrapper.like(HomeFlowSource::getName, name.trim());
        }
        if (Objects.nonNull(type)) {
            homeFlowSourceLambdaQueryWrapper.eq(HomeFlowSource::getType, type);
        }
        if (CollectionUtils.isNotEmpty(typeList)) {
            homeFlowSourceLambdaQueryWrapper.in(HomeFlowSource::getType, typeList);
        }
        if (StringUtils.isNotBlank(driver)) {
            homeFlowSourceLambdaQueryWrapper.like(HomeFlowSource::getDriver, driver.trim());
        }
        if (StringUtils.isNotBlank(address)) {
            homeFlowSourceLambdaQueryWrapper.like(HomeFlowSource::getAddress, address.trim());
        }
        Page<HomeFlowSource> pageParam = new Page<>(homeFlowSourceQo.getPageNum(), homeFlowSourceQo.getPageSize());
        pageParam.setOptimizeCountSql(false);
        Page<HomeFlowSource> homeFlowSourcePage = this.page(pageParam, homeFlowSourceLambdaQueryWrapper);
        return BeanConvertUtils.convert(homeFlowSourcePage, Page.class, (s, t) -> t.setRecords(BeanConvertUtils.convertList(homeFlowSourcePage.getRecords(), HomeFlowSourceVo.class)));
    }

    @Override
    public HomeFlowSourceVo getVoById(String id) {
        HomeFlowSource homeFlowSource = this.getById(id);
        if (Objects.isNull(homeFlowSource)) {
            throw new ServiceException("数据不存在");
        }
        return BeanConvertUtils.convert(homeFlowSource, HomeFlowSourceVo.class);
    }

    @Override
    public void connectionTest(HomeFlowSourceBo homeFlowSourceBo) {
        HomeFlowSourceBo.TypeEnum typeEnum = HomeFlowSourceBo.TypeEnum.getEnumByCode(homeFlowSourceBo.getType());
        if (typeEnum == HomeFlowSourceBo.TypeEnum.API) {
            throw new ServiceException("不支持连接测试");
        } else {
            String id = homeFlowSourceBo.getId();
            String password = homeFlowSourceBo.getPassword();
            if (StringUtils.isBlank(password) || Constants.PASSWORD_MASK.equals(password)) {
                if (StringUtils.isBlank(id)) {
                    throw new ServiceException("密码不能为空");
                } else {
                    HomeFlowSource homeFlowSource = this.getById(id);
                    if (Objects.isNull(homeFlowSource)) {
                        throw new ServiceException("数据不存在");
                    } else {
                        homeFlowSourceBo.setPassword(homeFlowSource.getPassword());
                    }
                }
            }
            HomeFlowSource homeFlowSource = BeanConvertUtils.convert(homeFlowSourceBo, HomeFlowSource.class);
//            DbUtil.close(DbUtils.getConnection(homeFlowSource));
//            DbUtils.removeDataSource(homeFlowSource);
            DbUtil.close(DbUtils.getSingleConnection(homeFlowSource));
        }
    }

    /**
     * 每天4点执行一次，清理7天内未使用的数据源
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void cleanUnusedDataSource() {
        DbUtils.cleanUnusedDataSource();
    }

}




